{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99731b7a-7c8d-4256-9eb3-42144a89405d",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip3 install google-search-results numexpr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a9f7ace5-37fd-4197-8fa5-bcb5855fa65c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.agents import AgentType, initialize_agent, load_tools\n",
    "from langchain.agents.tools import Tool\n",
    "from langchain.chains import LLMMathChain\n",
    "from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner\n",
    "from langchain.utilities import SerpAPIWrapper\n",
    "from langchain_openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6c629c42-01aa-4a11-8bd4-a0ed9043354f",
   "metadata": {},
   "outputs": [],
   "source": [
    "api_key = 'OPEN_API_KEY' # set your OPENAI API key\n",
    "serp_api_key= 'SERP API KEY' # set your SERPAPI key\n",
    "llm = OpenAI(api_key=api_key, temperature=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0e879dce-16aa-459c-821b-2121b95a3a8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_helper = SerpAPIWrapper(serpapi_api_key=serp_api_key)\n",
    "math_helper = LLMMathChain.from_llm(llm=llm, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76073fca-3319-4284-9875-c08e5e82024b",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_tool = Tool(name='Search', func=search_helper.run, description=\"use this tool to search for information\")\n",
    "math_tool = Tool(name='Calculator', func=math_helper.run, description=\"use this tool for mathematical calculations\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b7432132-5a66-4e52-adc0-930ad9b2406b",
   "metadata": {},
   "outputs": [],
   "source": [
    "tools = [search_tool, math_tool]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2474afef-a9e0-4546-961a-be2ab8c7430a",
   "metadata": {},
   "outputs": [],
   "source": [
    "action_planner = load_chat_planner(llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "83c29aae-6c3e-414e-8f9c-d847daa5d5c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = load_agent_executor(llm, tools, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "763bad90-24a8-4807-9f57-c3c503b7f1b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = PlanAndExecute(planner=action_planner, executor=agent_executor, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "479ff637-3cf2-4cfd-aef6-6ee3fe939369",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new PlanAndExecute chain...\u001b[0m\n",
      "steps=[Step(value='Gather data on the number of FIFA world cup wins for Brazil and France.'), Step(value='Calculate the difference between the two numbers.'), Step(value='Output the difference as the answer to the question.\\n')]\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mAction:\n",
      "{\n",
      "  \"action\": \"Search\",\n",
      "  \"action_input\": \"Number of FIFA world cup wins for Brazil and France\"\n",
      "}\n",
      "\n",
      "\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: Gather data on the number of FIFA world cup wins for Brazil and France.\n",
      "\n",
      "Response: Action:\n",
      "{\n",
      "  \"action\": \"Search\",\n",
      "  \"action_input\": \"Number of FIFA world cup wins for Brazil and France\"\n",
      "}\n",
      "\n",
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mThought: I can use the Calculator tool to subtract the number of wins for France from the number of wins for Brazil.\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Calculator\",\n",
      "  \"action_input\": \"5 - 2\"\n",
      "}\n",
      "```\n",
      "\u001b[0m\n",
      "\n",
      "\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n",
      "5 - 2\u001b[32;1m\u001b[1;3m```text\n",
      "5 - 2\n",
      "```\n",
      "...numexpr.evaluate(\"5 - 2\")...\n",
      "\u001b[0m\n",
      "Answer: \u001b[33;1m\u001b[1;3m3\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "\n",
      "Observation: \u001b[33;1m\u001b[1;3mAnswer: 3\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m I have the final answer now.\n",
      "Action:\n",
      "```\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"The difference between the number of FIFA world cup wins for Brazil and France is 3.\"\n",
      "}\n",
      "```\n",
      "\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: Calculate the difference between the two numbers.\n",
      "\n",
      "Response: The difference between the number of FIFA world cup wins for Brazil and France is 3.\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mAction:\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"The difference between the number of FIFA world cup wins for Brazil and France is 3.\"\n",
      "}\n",
      "\n",
      "\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n",
      "*****\n",
      "\n",
      "Step: Output the difference as the answer to the question.\n",
      "\n",
      "\n",
      "Response: Action:\n",
      "{\n",
      "  \"action\": \"Final Answer\",\n",
      "  \"action_input\": \"The difference between the number of FIFA world cup wins for Brazil and France is 3.\"\n",
      "}\n",
      "\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'input': 'How many more FIFA world cup wins does Brazil have compared to France?',\n",
       " 'output': 'Action:\\n{\\n  \"action\": \"Final Answer\",\\n  \"action_input\": \"The difference between the number of FIFA world cup wins for Brazil and France is 3.\"\\n}\\n\\n'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.invoke(\"How many more FIFA world cup wins does Brazil have compared to France?\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torchgpu",
   "language": "python",
   "name": "torchgpu"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
